﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using NAccounting.Models;
using NAccounting.QueryObjects;

namespace NAccounting.Controllers
{
    public class ProjectController : Controller
    {
        private NAccountEntities db = new NAccountEntities();

        public ActionResult Index() 
        {
            return View();
        }

        public ActionResult Edit(int id = 0)
        {
            var entity = db.Projects.SingleOrDefault(x => x.Id == id);

            if (entity == null) HttpNotFound();

            var model = Mapper.Map<Project, ProjectDetailsModel>(entity);

            return View(model);
        }

        public JsonResult GetActivities(int projectId)
        {
            var queryBuilder = new ProjectActivityQueryObject(db);
            queryBuilder.ProjectId = projectId;

            var pagedResult = queryBuilder.Get();

            var activities = db.ProjectActivities.Where(x => x.ProjectId == projectId).OrderBy(x => x.ExpectedToStart).ToArray();
            var result = (from a in activities
                          select new
                          {
                              id = a.Id,
                              name = a.Name,
                              series = new[] 
                              { 
                                new { 
                                    name = a.Description, 
                                    start = a.ExpectedToStart.ToString("yyyy-MM-dd"),
                                    end = a.ExpectedToEnd.ToString("yyyy-MM-dd")
                                }
                              }
                          });

            return Json(result.ToArray(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetProjects(string sidx, string sord, int page, int rows)
        {
            var queryObject = new ProjectQueryObject(db);

            var pageIndex = page - 1;
            var pagedResult = queryObject.Get(pageIndex, rows);

            var totalRecords = pagedResult.Total;
            var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);

            var jsonResult = new
            {
                totalPages = totalPages,
                page = page,
                records = totalRecords,
                rows = (
                    from e in pagedResult.List
                    let cellValues = new
                    {
                        Id = e.Id.ToString(),
                        Actions = "",
                        Name = e.Name,
                        Customer = e.Customer.Name,
                        canEdit = Boolean.TrueString.ToLower()
                    }
                    select new
                    {
                        id = e.Id,
                        cell = new string[]{
                             cellValues.Id, 
                             cellValues.Actions, 
                             cellValues.Name, 
                             cellValues.Customer,
                             cellValues.canEdit
                        }
                    }).ToArray()
            };

            return Json(jsonResult, JsonRequestBehavior.AllowGet);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
